package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.BusinessQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.FileUploadUtil;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.List;

@Controller
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private ServletContext servletContext;

    @RequestMapping("/list")
    @RequiredPermission(name = "门店页面", expression = "business:list")
    public String list(@ModelAttribute("qo") BusinessQueryObject qo, Model model) {
        PageInfo pageInfo = businessService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "business/list";
    }


    @RequestMapping("/delete")
    @RequiredPermission(name = "门店删除", expression = "business:delete")
    public String delete(Long id) {
        if (id != null) {
            businessService.deleteBusiness(id);
        }
        return "redirect:/business/list";
    }


    @RequestMapping("/input")
    @RequiredPermission(name = "门店新增或修改", expression = "business:saveOrUpdate")
    public String input(Long id, Model model) {
        //获取所有门店的数据
        if (id != null) {
            Business business = businessService.selectBusiness(id);
            model.addAttribute("business", business);
        }
        return "business/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "门店新增或修改", expression = "business:saveOrUpdate")
    public String saveOrUpdate(Business business, MultipartFile file) throws Exception {
        //参数接收 file 文件 判断有没有file文件
        if (file !=null && file.getSize() != 0) {
            //如果有的话就先获取绝对路径
            String realPath = servletContext.getRealPath("/");
            //再判断 之前有没有存储过文件 如果有的话就删除
            if(StringUtils.hasText(business.getLicenseImg())){
            //使用绝对路径 + 存储的路径
            FileUploadUtil.deleteFile(realPath + business.getLicenseImg());
            }
            //所以我们要通过FileUploadUtil的方法把文件存到本地 并返回路径
            //删除完在存储 相当于把Business对象的LicenseImg字段删除 然后在插入新的路径
            //这个对象再给service调用插入或更新  把路径存到数据库中
            business.setLicenseImg( FileUploadUtil.uploadFile(file, realPath));
            //小细节 如果我们没有上传文件的话 他会调用sql 把我们原有的数据设置为null
            //所以sql语句的时候我们就要对Business对象里面的LicenseImg字段判断是否为空
            //如果为空的话就不用修改 反之修改

            //修改的时候 我们获取的是回显数据里面的LicenseImg字段 所以要去页面设置hidden的 LicenseImg字段 也可以设置在a标签上
        }


        if (business.getId() != null) {
            businessService.updateBusiness(business);
        } else {
            businessService.insertBusiness(business);
        }
        return "redirect:/business/list";
    }
}
